const arr = [
    ["a", "b", "c"],
    ["a", "d"],
    ["d", "e"],
    ["f", "g"],
    ["h", "g"],
    ["g"],
    ["i"]
]


var set1 = new Set([1,2,3]);
var set2 = new Set([2,3,4]);

// 并集
let union = new Set([...set1, ...set2]);

// 交集
let intersect = new Set([...set1].filter( x => set2.has(x)));

// 差集
let difference = new Set([...set1].filter(x => !set2.has(x)));


// [a,b],[c,d],[b,c]
// 并查集

// [
//     ["a", "b", "c", "d", "e"],
//     ["f", "g", "h"],
//     ["i"]
// ]

// 将有交集的数组合并
// 判断是否有交集
function transform(arr) {
    let res = []
    arr = arr.map(el => el.sort()).sort()
    const item = arr.reduce((pre, cur) => {
        if (cur.some(el => pre && pre.includes(el))) {
            pre = pre.concat(cur)
        } else {
            res.push(pre)
            pre = cur
        }
        return [...new Set(pre)]
    })
    res.push(item)
    return res;
}
console.log(transform(arr))




function fn(arr) {
    arr = arr.map(el => el.sort()).sort()
    let temp = new Set(arr[0]);
    let result = []
    for (let i = 1; i < arr.length; i++) {
        let isConbin = [...temp].some(el => arr[i].includes(el))
        if (!isConbin) {
            result.push(Array.from(temp).sort((a, b) => a - b));
            temp = new Set(arr[i])
        } else {
            temp = new Set([...temp, ...arr[i]])
        }
    }
    result.push(Array.from(temp).sort((a, b) => a - b));


    return result

}

const arr2 = [
    ["a", "b", "c"],
    ["a", "d"],
    ["d", "e"],
    ["f", "g"],
    ["h", "g"],
    ["g"],
    ["i"]
]
console.log(fn(arr2))